package com.yc.txx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.txx.entity.Car;
import com.yc.txx.mapper.CarMapper;
import com.yc.txx.service.ICarService;
import com.yc.txx.service.BizException;
import com.yc.txx.util.Utils;
import com.yc.txx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 天下行租车-车辆表-10 前端控制器
 * </p>
 *
 * @author xl
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/txx/car")
public class CarController {

    @Autowired
    CarMapper carMapper;

    JedisPool jedisPool = new JedisPool("127.0.0.1",6379);
    ObjectMapper mapper = new ObjectMapper();

    @Autowired
    ICarService carService;

    @RequestMapping("query")
    List<Car> query(Car car) throws IOException {
        if(car.getPrice().intValue() == 3 &&
                Utils.isEmpty(car.getModel() ) &&
                Utils.isEmpty(car.getBrand()) &&
                Utils.isEmpty(car.getSort())
        ) {
            Jedis jedis = jedisPool.getResource();
            try{
                if(jedis.exists("cars")){
                    String json = jedis.get("cars");
                    List list = mapper.readValue(json, List.class);
                    System.out.println("--------- redis cars -----------");
                    return list;
                } else {
                    List<Car> list = carMapper.select(car);
                    String json = mapper.writeValueAsString(list);
                    jedis.set("cars",json);
                    System.out.println("--------- mysql cars -----------");
                    return list;
                }
            }finally {
                jedisPool.returnResource(jedis);
            }
        } else {
            System.out.println("--------- mysql cars -----------");
            return carMapper.select(car);
        }
//        return carMapper.select(car);
    }

    @RequestMapping("queryBrand")
    List<String> queryBrand() throws IOException {
        Jedis jedis = jedisPool.getResource();
        try{
            if(jedis.exists("brandItems")) {
                String json = jedis.get("brandItems");
                List list = mapper.readValue(json, List.class);
                return list;
            } else {
                List<String> list = carMapper.selectBrand();
                String json = mapper.writeValueAsString(list);
                jedis.set("brandItems",json);
                return list;
            }
        }finally {
            jedisPool.returnResource(jedis);
        }

//        return carMapper.selectBrand();
    }

    @RequestMapping("selectCarDetail")
    Car selectCarDetail(String id){
        return carMapper.selectCarDetail(id);
    }


    @RequestMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("上传文件的字段名(请求参数) file.getName() = " + file.getName());
        System.out.println("上传文件名 file.getOriginalFilename() = " + file.getOriginalFilename());

        String NewName = UUID.randomUUID().toString();
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File fileObj = new File("D:\\yc三期项目\\新教学视频\\5.14\\upload\\" + NewName + fileName);
        file.transferTo(fileObj);
        // 单文件上传
        return Result.ok("文件上传成功!",NewName+fileName);
    }

    @RequestMapping("addCar")
    public Result addCar(@RequestBody @Valid Car car,Errors errors) throws BizException{
        if(errors.hasErrors()){
            return Result.fail("添加车辆失败",errors.getAllErrors());
        }
        try {
            carService.addCar(car);
            return Result.ok("添加车辆成功",car);
        } catch (BizException e) {
            String fieldName = e.getData().toString();
            errors.rejectValue(fieldName,fieldName + ".exists",e.getMessage());
            throw new BizException("字段验证失败",errors.getAllErrors());
        }
    }
}
